// pages/common/school-search/index.js
import {getList} from "../../../service/College";
Page({

    /**
     * 页面的初始数据
     */
    data: {
        value: "",
        school: {
            loading: false,
            reset: false,
            error: false,
            total: -1,
            list: []
        },
        current: -1
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /*
    * 简体列表加载
    * */
    onLoadList: async function (e) {
        try {
            let search = this.data._search;
            let paging = e.detail.paging;
            let list = this.data.school.list;
            let isReset = this.data.school.reset;
            let res = await getList({
                page: paging.page,
                limit: paging.pageSize,
                sName: search
            });
            let newList = res.data;
            list = isReset ? newList : list.concat(newList);
            this.setData({
                "school.list": list,
                "school.total": res.count
            });
        } catch (e) {
            this.setData({
                "school.error": true
            })
        } finally {
            this.setData({
                "school.reset": false,
                "school.loading": false
            })
        }
    },

    /*
    * 搜索
    * */
    onSearch: function (e) {
        this.data._search = e.detail;
        this.setData({
            "school.list": [],
            "school.reset": true
        })
    },
    onCancel: function () {
        wx.navigateBack()
    },

    /*
    * 选择学校
    * */
    bindSelected: function (e) {
        const that = this;
        let index = e.currentTarget.dataset.index;
        let list = this.data.school.list;
        let current = this.data.current;
        if (index !== current) {
            this.setData({current: index});
            clearTimeout(this.TIME_OUT);
            this.TIME_OUT = setTimeout(function () {
                const eventChannel = that.getOpenerEventChannel();
                eventChannel.emit("acceptDataFromOpenedPage", list[index]);
                wx.navigateBack();
            }, 500)
        }
    },


    /*
    * 页面触底事件
    * */
    onReachBottom: function () {
        this.setData({"school.loading": true})
    }
})